#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 2e3 + 5, inf = 0x3f3f3f3f;
int n, k, siz[N];
int e_cnt, heads[N];
ll f[N][N];

struct Edge {
  int v, nxt, w;
} e[N << 1];

inline void add(int u, int v, int w) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt, e[e_cnt].w = w;
}

inline int min(int x, int y) { return x < y ? x : y; }

inline void max(ll &x, ll y) {
  if (y > x) x = y;
}

void solve(int u, int fa) {
  siz[u] = 1;
  for (int i = heads[u], v; i; i = e[i].nxt)
    if ((v = e[i].v) != fa) {
      solve(v, u);
      for (int j = siz[u]; ~j; --j)
        for (int h = siz[v]; ~h; --h)
          max(f[u][j + h], f[u][j] + f[v][h] +
                               (1ll * h * (k - h) +
                                1ll * (siz[v] - h) * (n - k + h - siz[v])) *
                                   e[i].w);
      siz[u] += siz[v];
    }
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("coloration.in", "r", stdin);
  freopen("coloration.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &k);
  for (int i = 1, u, v, w; i < n; ++i) {
    scanf("%d%d%d", &u, &v, &w);
    add(u, v, w), add(v, u, w);
  }
  solve(1, 1);
  printf("%lld", f[1][k]);
  return 0;
}